// 加载Express模块
const express = require('express');

// 加载MySQL模块
const mysql = require('mysql');

// 加载bodyParser模块
const bodyParser = require('body-parser');

// 加载MD5模块
const md5 = require('md5');

// 创建MySQL连接池
const pool = mysql.createPool({
  host: '127.0.0.1', //MySQL服务器地址
  port: 3306, //MySQL服务器端口号
  user: 'root', //数据库用户的用户名
  password: '', //数据库用户密码
  database: 'baodian', //数据库名称
  connectionLimit: 20, //最大连接数
  charset: 'utf8' //数据库服务器的编码方式
});
// 创建服务器对象
const server = express();

server.use(bodyParser.urlencoded({
  extended: false
}));


// 加载CORS模块
const cors = require('cors');
const {
  query
} = require('express');

// 使用CORS中间件
server.use(cors({
  origin: ['http://localhost:8080', 'http://127.0.0.1:8080']
}));

// 获取所有驾校的接口
server.get('/jiaxiao', (req, res) => {
  // 获取客户端传递的id参数
  // let id = req.query.id;

  // SQL语句以获取驾校的数据
  let sql = 'select * from jiaxiao';
  // 执行SQL语句
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({
      message: 'ok',
      code: 200,
      results: results
    });
  });
});



server.get('/jiaolian', (req, res) => {
  // 获取客户端传递的id参数
  // let id = req.query.id;

  // SQL语句以获取驾校的数据
  let sql = 'select * from jiaolian';
  // 执行SQL语句
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({
      message: 'ok',
      code: 200,
      results: results
    }); 
  });
});





server.get('/user', (req, res) => {
  let sql = 'select * from user';
  // 执行SQL语句
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({
      message: 'ok',
      code: 200,
      results: results
    });
  });
});


server.get('/test', (req, res) => {

  // SQL语句以获取驾校的数据
  let sql = 'select * from test';
  // 执行SQL语句
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({
      message: 'ok',
      code: 200,
      results: results
    });
  });
});





server.get('/test1', (req, res) => {
  // 获取客户端传递的aid参数
  // let aid = req.query.aid;
  // console.log(req.query);
  // SQL语句以获取驾校的数据
  let sql = 'select * from test where aid=?';
  // 执行SQL语句
  pool.query(sql, [req.query.aid], (error, results) => {
    if (error) throw error;
    res.send({
      message: 'ok',
      code: 200,
      results: results
    });
  });
});




server.get('/comment', (req, res) => {
  // 获取客户端传递的id参数
  // let id = req.query.id;
  // SQL语句以获取驾校的数据
  let sql = 'select * from comment';
  // 执行SQL语句
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({
      message: 'ok',
      code: 200,
      results: results
    });
  });
});


/*登录*/
server.post("/username", (req, res) => {
  let obj = req.body;
  // console.log(req.body);
  //查找用户是否存在
  let sql = "SELECT * FROM user WHERE username=?";
  pool.query(sql, [obj.username], (error, results) => {
    if (error) throw error;
    // 如果用户不存才，则插入记录
    if (results[0]) {
      //否则产生和里的错误提示
      res.send({
        message: "用户已经存在，注册失败",
        code: 0
      });
    } else {
      // console.log('用户注册成功');
      sql = "INSERT INTO user set ?";
      pool.query(sql, [obj], (error, results) => {
        if (error) throw error;
        res.send({
          message: "注册成功",
          code: 1
        });
      });
    }
  });
});
/*登录 */
server.post("/denglu", (req, res) => {
  let obj = req.body;
  let sql = "INSERT INTO user set username=?";
  // console.log(obj.username,obj.password);
  pool.query(sql, [obj.username], (error, results) => {
     if(error) throw  error;
     res.send({
      message: "登录成功",
      code: 200
    });
  })  
})

/*收藏*/
server.post("/shoucang", (req, res) => {
  let obj = req.body;
  // console.log(req.body);
  //查找用户是否存在
  let sql = "SELECT * FROM shoucang WHERE aid=?";
  pool.query(sql, [obj.aid], (error, results) => {
    if (error) throw error;
    // 如果用户不存才，则插入记录
    if (results[0]) {
      //否则产生和里的错误提示
      res.send({
        message: "题目已存在，收藏失败",
        code: 0
      });
    } else {
      sql = "INSERT INTO shoucang set aid=?";
      pool.query(sql, [obj.aid], (error, results) => {
        if (error) throw error;
        res.send({
          message: "收藏成功",
          code: 1
        });
      });
    }
  });
});

server.get("/shoucang1",(req,res)  => {
  let sql = "SELECT * FROM shoucang ";
  pool.query(sql,(error,results)=>{
    if(error) throw error;
    res.send({message:"收藏", code:200,results:results})
  })
})

// 指定服务器对象监听的端口号
server.listen(3000, () => {
  console.log('server is running...');
});